package com.ztmg.water.service.apartment.impl;

import com.ztmg.water.mapper.apartment.ApartmentFloorMapper;
import com.ztmg.water.mapper.apartment.ApartmentRoomsMapper;
import com.ztmg.water.model.apartment.ApartmentFloor;
import com.ztmg.water.model.apartment.ApartmentInfo;
import com.ztmg.water.model.apartment.ApartmentRooms;
import com.ztmg.water.vo.apartment.ApartmentInfoVO;
import com.ztmg.water.dto.apartment.ApartmentInfoDTO;
import com.ztmg.water.dto.apartment.ApartmentInfoPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.apartment.ApartmentInfoService;
import com.ztmg.water.mapper.apartment.ApartmentInfoMapper;import com.ztmg.water.model.apartment.ApartmentInfoExample;

@Service
public class ApartmentInfoServiceImpl implements ApartmentInfoService{

    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private ApartmentFloorMapper apartmentFloorMapper;
    @Resource
    private ApartmentRoomsMapper  apartmentRoomsMapper;

    @Override
    public JsonResult createApartment(ApartmentInfoDTO dto) {
        if(null != dto){
            ApartmentInfo bean = new ApartmentInfo();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            apartmentInfoMapper.insert(bean);
            int apartId =bean.getId();
            //根据楼层数,房间数自动添加楼层、房间数据
            int floors = bean.getApartmentFloors();//公寓层数
            int rooms = bean.getApartmentRooms();//每层房间数
            Format f3 = new DecimalFormat("000");
            Format f2 = new DecimalFormat("00");
            if(floors>0)
            {
                for (int i=0;i<floors;i++)
                {
                    ApartmentFloor floorBean = new ApartmentFloor();
                    floorBean.setApartmentId(apartId);
                    floorBean.setCreateTime(new Date());
                    floorBean.setFloorName(bean.getApartmentSn()+"-"+f2.format(i+1));
                    floorBean.setFloorSn(bean.getApartmentSn()+"-"+f2.format(i+1));
                    floorBean.setFloorRooms(rooms);
                    floorBean.setRemark(bean.getApartmentSn()+"-"+f2.format(i+1));
                    apartmentFloorMapper.insert(floorBean);
                    int floorId = floorBean.getId();
                    if(rooms>0)
                    {
                        for (int j=0;j<rooms;j++)
                    {
                        ApartmentRooms roomsBean = new ApartmentRooms();
                        roomsBean.setApartmentId(apartId);
                        roomsBean.setCreateTime(new Date());
                        roomsBean.setFloorId(floorId);
                        roomsBean.setModifyTime(new Date());
                        roomsBean.setRemark(floorBean.getFloorSn()+"-"+f3.format(j+1));
                        roomsBean.setRoomName(floorBean.getFloorSn()+"-"+f3.format(j+1));
                        roomsBean.setRoomNumber(String.valueOf(j+1));
                        roomsBean.setRoomSn(floorBean.getFloorSn()+"-"+f3.format(j+1));
                        apartmentRoomsMapper.insert(roomsBean);
                    }

                    }

                }
            }

            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult create(ApartmentInfoDTO dto) {
        if(null != dto){
            ApartmentInfo bean = new ApartmentInfo();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
           apartmentInfoMapper.insert(bean);

            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(ApartmentInfoDTO dto) {
        if(null != dto && null != dto.getId()){
            ApartmentInfo bean = new ApartmentInfo();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            apartmentInfoMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<ApartmentInfoVO> selectById(Integer id) {
        if(null != id){
            ApartmentInfo bean = apartmentInfoMapper.selectByPrimaryKey(id);
            ApartmentInfoVO vo = new ApartmentInfoVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<List<ApartmentInfoVO>> select(ApartmentInfoDTO dto) {
        ApartmentInfoExample example = new ApartmentInfoExample();
        ApartmentInfoExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */

        List<ApartmentInfo> beanList = apartmentInfoMapper.selectByExample(example);
        List<ApartmentInfoVO> respDTOS = beanList.parallelStream().map((bean) -> {
            ApartmentInfoVO vo = new ApartmentInfoVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());;
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<PageInfo<ApartmentInfoVO>> selectPage(ApartmentInfoPageDTO dto) {
        ApartmentInfoExample example = new ApartmentInfoExample();
        ApartmentInfoExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<ApartmentInfo> beanList = apartmentInfoMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<ApartmentInfoVO> respDTOS = beanList.parallelStream().map((bean) -> {
            ApartmentInfoVO vo = new ApartmentInfoVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( apartmentInfoMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(ApartmentInfoDTO dto) {
        ApartmentInfoExample example = new ApartmentInfoExample();
        ApartmentInfoExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(apartmentInfoMapper.deleteByExample(example));
    }
}